package com.yzz.hzraisedust.hzraseidust.main.squaredetail;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yzz.hzraisedust.hzraseidust.R;
import com.yzz.hzraisedust.hzraseidust.main.BaseActivity;
import com.yzz.hzraisedust.hzraseidust.modelbean.AlarmItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.RequstItemBean;
import com.yzz.hzraisedust.hzraseidust.modelbean.SquareDetailItemBean;
import com.yzz.hzraisedust.hzraseidust.util.Constant;
import com.yzz.hzraisedust.hzraseidust.util.JsonUtil;
import com.yzz.hzraisedust.hzraseidust.util.NetUtil;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class SquareDetailInfoActivity extends BaseActivity implements Response.Listener<String>,Response.ErrorListener,AdapterView.OnItemClickListener{

    private AlarmItemBean mAlarmItemBean;

    private ListView mListView;
    private RequstItemBean<SquareDetailItemBean> squareItems;
    private TextView mTitleTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_square_detail_info);
        initLoginingDlg();
        showLoginingDlg();
        initView();
        initData();
        mTitleTextView.setText(mAlarmItemBean.getCityName());
    }

    private void initView() {
        View view = findViewById(R.id.back_include);
        ImageView back = view.findViewById(R.id.back_to_pri_imageview);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTitleTextView = view.findViewById(R.id.title_all_textview);
        mListView = findViewById(R.id.square_item_alram_info_listView);
        mListView.setEmptyView(findViewById(R.id.square_empty_list_textview));
        mListView.setOnItemClickListener(this);
    }



    /**
     * 初始化数据及加载网络数据
     */
    private void initData() {
        mAlarmItemBean =(AlarmItemBean) getIntent().getSerializableExtra(Constant.ALARM_ITEM_BEAN);
        Map<String,String> params = new HashMap<>();
        params.put(Constant.UID_LOWER_KEY,Constant.Uid);
        params.put(Constant.AREA_CODE_KEY,mAlarmItemBean.getCityCode());
        NetUtil netUtil = new NetUtil(this);
        netUtil.requestData(this,this,Constant.SQUARE_ITEM_DETAIL_URL,params);

    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        Toast.makeText(this,"发生错误",Toast.LENGTH_LONG).show();
        closeLoginingDlg();
        finish();
    }

    @Override
    public void onResponse(String s) {

        Type type = new TypeToken<RequstItemBean<SquareDetailItemBean>>(){
        }.getType();
        squareItems= (RequstItemBean<SquareDetailItemBean>) JsonUtil.jsonToObject(s,type);
        if(squareItems == null || !squareItems.isSuccess()) {
           Toast.makeText(SquareDetailInfoActivity.this,"发生错误",Toast.LENGTH_LONG).show();
            closeLoginingDlg();
            finish();
            return;
        }
        refreshListView();
        closeLoginingDlg();
    }

    private void refreshListView() {
        CAdapter cAdapter = new CAdapter();
        mListView.setAdapter(cAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        SquareDetailItemBean itemBean = squareItems.getRows().get(i);
        Intent intent = new Intent(SquareDetailInfoActivity.this,ProjectAlarmDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.ALARM_PROJECT_ITEM_BEAN,itemBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    class CAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return squareItems.getRows().size();
        }

        @Override
        public Object getItem(int i) {
            return squareItems.getRows().get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if(view == null){
                view = LayoutInflater.from(SquareDetailInfoActivity.this).inflate(R.layout.square_item_detail_item_value_layout,null,false);
                viewHolder = new ViewHolder();
                viewHolder.projectNameTextView = view.findViewById(R.id.square_item_detail_project_name_textview);
                viewHolder.alarmCountTextView = view.findViewById(R.id.square_item_detail_project_alarm_count_name_textview);
                view.setTag(viewHolder);
            }
            if(viewHolder == null){
                viewHolder = (ViewHolder) view.getTag();
            }
            SquareDetailItemBean detailItemBean = squareItems.getRows().get(i);
            viewHolder.projectNameTextView.setText(detailItemBean.getName());
            viewHolder.alarmCountTextView.setText(""+detailItemBean.getAlarmNum());
            return view;
        }
        class ViewHolder{
             TextView projectNameTextView;
            TextView alarmCountTextView;
        }
    }
}
